import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AuthSiteService } from '../../services/admin/auth-site.service';

/**
 * SiteController
 * 对应 PHP: Site Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('home')
@Controller('adminapi/home')
export class SiteController {
  constructor(private readonly authSiteService: AuthSiteService) {}

  /**
   * 站点管理
   * 路由: GET site
   * PHP路由: Route::get('site', 'home.Site/lists')
   */
  @Get('site')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.authSiteService.getSitePage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET site/:id
   * PHP路由: Route::get('site/:id', 'home.Site/info')
   */
  @Get('site/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async info(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.authSiteService.getSiteInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: PUT site/:id
   * PHP路由: Route::put('site/:id', 'home.Site/edit')
   */
  @Put('site/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.authSiteService.editSite(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET site/group
   * PHP路由: Route::get('site/group', 'home.Site/getSiteGroup')
   */
  @Get('site/group')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async getSiteGroup(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getSiteGroup

      return await this.authSiteService.getSiteGroup();
    } catch (error) {
      throw new BusinessException('getSiteGroup操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET site/group/app_list
   * PHP路由: Route::get('site/group/app_list', 'home.Site/getSiteGroupAppList')
   */
  @Get('site/group/app_list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async getSiteGroupAppList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getSiteGroupAppList

      return await this.authSiteService.getSiteGroupAppList();
    } catch (error) {
      throw new BusinessException('getSiteGroupAppList操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: POST site/create
   * PHP路由: Route::post('site/create', 'home.Site/create')
   */
  @Post('site/create')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async create(@Body() data: CreateDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: create

      return await this.authSiteService.createSite(data);
    } catch (error) {
      throw new BusinessException('create操作失败', error);
    }
  }
}
